# -*- coding:utf-8 -*-
'''
export databases from mysql server on windows 7
'''

import sys,os
import MySQLdb
import MyPyLib
import ConfigParser

dbs=['taobaoapi','tmall','tshopdata','weibo','zhihu']

def getConfig():
	config=ConfigParser.RawConfigParser(allow_no_value=True)
	with open('C:/mysql.ini') as f:
		config.readfp(f)
	return config

def export_db(db,path):
	#call command windows and run sqldump command
	#if os.path.exists(path):os.mkdir(path)
	config=getConfig()
	cmd='mysqldump -u %s --password="%s" --no-data %s >%s'%(config.get('mysqldroot','user'),
		config.get('mysqldroot','pw'),
		db,os.path.join(path,'%s.sql'%db))
	os.system(cmd)

def get_dbs():
	db=MyPyLib.getConn()
	db.query('show databases')
	r=db.store_result()
	dbs=list()
	while r.row_tell()>=0:
		dbs.append(r.fetch_row()[0][0])
	db.close()
	return dbs

def main():
	path=sys.argv[1]
	if not os.path.exists(path):
		try:
			os.mkdir(path)
			print 'Path %s created'%path
		except:
			print 'Error Path'
			sys.exit(0)
	for db in get_dbs():
		if not db in ['information_schema','mysql','test','performance_schema']:
			export_db(db,path)
	pass

if __name__ == '__main__':
	main()
	pass
